home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / io / CharArrayWriter.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  4.3 KB  |  176 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)CharArrayWriter.java    1.11 98/06/29
  3.  *
  4.  * Copyright 1996-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17. /**
  18.  * This class implements a character buffer that can be used as an Writer.
  19.  * The buffer automatically grows when data is written to the stream.  The data
  20.  * can be retrieved using toCharArray() and toString().
  21.  *
  22.  * @author    Herb Jellinek
  23.  * @version     1.11, 06/29/98
  24.  * @since       JDK1.1
  25.  */
  26. public
  27. class CharArrayWriter extends Writer {
  28.     /**
  29.      * The buffer where data is stored.
  30.      */
  31.     protected char buf[];
  32.  
  33.     /**
  34.      * The number of chars in the buffer.
  35.      */
  36.     protected int count;
  37.  
  38.     /**
  39.      * Creates a new CharArrayWriter.
  40.      */
  41.     public CharArrayWriter() {
  42.     this(32);
  43.     }
  44.  
  45.     /**
  46.      * Creates a new CharArrayWriter with the specified initial size.
  47.      *
  48.      * @exception IllegalArgumentException if initialSize is negative
  49.      */
  50.     public CharArrayWriter(int initialSize) {
  51.         if (initialSize < 0) {
  52.             throw new IllegalArgumentException("Negative initial size: "
  53.                            + initialSize);
  54.         }
  55.     buf = new char[initialSize];
  56.     }
  57.  
  58.     /**
  59.      * Writes a character to the buffer.
  60.      */
  61.     public void write(int c) {
  62.     synchronized (lock) {
  63.         int newcount = count + 1;
  64.         if (newcount > buf.length) {
  65.         char newbuf[] = new char[Math.max(buf.length << 1, newcount)];
  66.         System.arraycopy(buf, 0, newbuf, 0, count);
  67.         buf = newbuf;
  68.         }
  69.         buf[count] = (char)c;
  70.         count = newcount;
  71.     }
  72.     }
  73.  
  74.     /**
  75.      * Writes characters to the buffer.
  76.      * @param c    the data to be written
  77.      * @param off    the start offset in the data
  78.      * @param len    the number of chars that are written
  79.      */
  80.     public void write(char c[], int off, int len) {
  81.     if ((off < 0) || (off > c.length) || (len < 0) ||
  82.             ((off + len) > c.length) || ((off + len) < 0)) {
  83.         throw new IndexOutOfBoundsException();
  84.     } else if (len == 0) {
  85.         return;
  86.     }
  87.     synchronized (lock) {
  88.         int newcount = count + len;
  89.         if (newcount > buf.length) {
  90.         char newbuf[] = new char[Math.max(buf.length << 1, newcount)];
  91.         System.arraycopy(buf, 0, newbuf, 0, count);
  92.         buf = newbuf;
  93.         }
  94.         System.arraycopy(c, off, buf, count, len);
  95.         count = newcount;
  96.     }
  97.     }
  98.  
  99.     /**
  100.      * Write a portion of a string to the buffer.
  101.      * @param  str  String to be written from
  102.      * @param  off  Offset from which to start reading characters
  103.      * @param  len  Number of characters to be written
  104.      */
  105.     public void write(String str, int off, int len) {
  106.     synchronized (lock) {
  107.         int newcount = count + len;
  108.         if (newcount > buf.length) {
  109.         char newbuf[] = new char[Math.max(buf.length << 1, newcount)];
  110.         System.arraycopy(buf, 0, newbuf, 0, count);
  111.         buf = newbuf;
  112.         }
  113.         str.getChars(off, off + len, buf, count);
  114.         count = newcount;
  115.     }
  116.     }
  117.  
  118.     /**
  119.      * Writes the contents of the buffer to another character stream.
  120.      * @param out    the output stream to write to
  121.      */
  122.     public void writeTo(Writer out) throws IOException {
  123.     synchronized (lock) {
  124.         out.write(buf, 0, count);
  125.     }
  126.     }
  127.  
  128.     /**
  129.      * Resets the buffer so that you can use it again without
  130.      * throwing away the already allocated buffer.
  131.      */
  132.     public void reset() {
  133.     count = 0;
  134.     }
  135.  
  136.     /**
  137.      * Returns a copy of the input data.
  138.      */
  139.     public char toCharArray()[] {
  140.     synchronized (lock) {
  141.         char newbuf[] = new char[count];
  142.         System.arraycopy(buf, 0, newbuf, 0, count);
  143.         return newbuf;
  144.     }
  145.     }
  146.  
  147.     /**
  148.      * Returns the current size of the buffer.
  149.      */
  150.     public int size() {
  151.     return count;
  152.     }
  153.  
  154.     /**
  155.      * Converts input data to a string.
  156.      * @return the string.
  157.      */
  158.     public String toString() {
  159.     synchronized (lock) {
  160.         return new String(toCharArray());
  161.     }
  162.     }
  163.  
  164.     /**
  165.      * Flush the stream.
  166.      */
  167.     public void flush() { }
  168.  
  169.     /**
  170.      * Close the stream.  This method does not release the buffer, since its
  171.      * contents might still be required.
  172.      */
  173.     public void close() { }
  174.  
  175. }
  176.